import HTTP_STATUS_CODE from 'http-status-codes'
import { message } from 'antd'

import * as request from '../constant/fetchRequest'

export const createUser = user => {
  return dispatch => {
    (async () => {
      delete user.prefix
      delete user.confirm
      const res = await request.post(`../user/register`, user)
      if (res.status === HTTP_STATUS_CODE.CREATED || res.status === HTTP_STATUS_CODE.FORBIDDEN) {
        window.location.href = `../home/index.html`
      }
      else {
        message.error(res.body.message)
      }
    })()
  }
}
export const getSocialInfo = () => {
    return dispatch => {
        (async () => {
            const res = await request.get(`../social/info`)
            if (res.status === HTTP_STATUS_CODE.OK) {
                dispatch({type: 'GET_SOCIAL_INFO', data: res.body})
            }
            else {
                message.error(res.body.message)
            }
        })()
    }
}
export const submitEmail = (email) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`./api/password-reset`, email)
      if (res.status === HTTP_STATUS_CODE.CREATED) {
        message.success('邮件发送成功，请注意查收！')
      } else {
        message.error(res.body.message)
      }
    })()
  }
}

export const submitPassword = (passwordInfo) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`./api/password-reset`, passwordInfo)
      if (res.status === HTTP_STATUS_CODE.NO_CONTENT) {
        message.success('重置密码成功，请重新登录')
        setTimeout("window.location.href='../auth/login'",2000)
      } else {
        message.error(res.body.message)
      }
    })()
  }
}



